package cn.dwy2002.binaryhandwritingserver.controller;

import cn.dwy2002.binaryhandwritingserver.common.Result;
import cn.dwy2002.binaryhandwritingserver.exception.ArticleNotFoundException;
import cn.dwy2002.binaryhandwritingserver.pojo.dto.ArticleDTO;
import cn.dwy2002.binaryhandwritingserver.pojo.vo.ArticleVO;
import cn.dwy2002.binaryhandwritingserver.service.service.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章管理
 *
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/05/31
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/articles")
public class ArticleController {
    private final ArticleService articleService;

    /**
     * 获取所有文章
     *
     * @return 文章列表
     */
    @GetMapping
    public Result<List<ArticleVO>> getAllArticles() {
        return articleService.getAllArticles();
    }

    /**
     * 获取文章详情
     *
     * @param id 文章ID
     * @return 文章详情
     * @throws ArticleNotFoundException 文章不存在时抛出
     */
    @GetMapping("/{id}")
    public Result<ArticleVO> getArticleById(@PathVariable Long id) throws ArticleNotFoundException {
        return articleService.getArticleById(id);
    }

    /**
     * 创建新文章
     *
     * @param dto 文章数据
     * @return 创建结果
     */
    @PostMapping
    public Result<Void> createArticle(@RequestBody ArticleDTO dto) {
        return articleService.createArticle(dto);
    }

    /**
     * 更新文章
     *
     * @param dto 文章数据
     * @return 更新结果
     * @throws ArticleNotFoundException 文章不存在时抛出
     */
    @PutMapping
    public Result<Void> updateArticle(@RequestBody ArticleDTO dto) throws ArticleNotFoundException {
        return articleService.updateArticle(dto);
    }

    /**
     * 删除文章
     *
     * @param id 文章ID
     * @return 删除结果
     * @throws ArticleNotFoundException 文章不存在时抛出
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteArticle(@PathVariable Long id) throws ArticleNotFoundException {
        return articleService.deleteArticle(id);
    }
}
